﻿var ns = namespace("kaskelot.sp.ui");

ns.bemanningvaldistriktdetails = function () {

	function init() {
		getValdistrikt();
		getBemanning();
	}

	function getValdistrikt() {
		var id = getParameterByName('ValdistriktId');

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/GetValdistriktDetails",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetValdistriktDetailsResult.HasError) {
						$("#title-container").html(
							data.GetValdistriktDetailsResult.Data.ValdistriktNamn
						);

						$("#grundinfo-results").html(
							$("#grundinfo-template").render(data.GetValdistriktDetailsResult.Data)
						);

						$("#andradav-container").html(
							$("#andradav-template").render(data.GetValdistriktDetailsResult.Data)
						);
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getBemanning() {
		var id = getParameterByName('ValdistriktId');

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/GetBemanning",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetBemanningResult.HasError) {
						$("#bemanning-results tbody").html(
							$("#bemanning-template").render(data.GetBemanningResult.Data)
						);
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function removePerson(id) {
		kaskelot.sp.ui.prompts.showWarningPrompt({
			text: "Kom ihåg att ta bort eventuella kurser för personen du tar bort.",
			okFunction: function () {
				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/RemovePersonFromBemanningValdistrikt",
					type: "GET",
					data: { id: id },
					processData: true,
					cache: false,
					success:
						function (data) {
							if (!data.RemovePersonFromBemanningValdistriktResult.HasError) {
								getBemanning();
							}
						},
					error:
						function (xhr) {
							kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
						}
				});
			}
		});
	}

	function removeBemanning(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/DeleteBemanningValdistrikt",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.DeleteBemanningValdistriktResult.HasError) {
						getBemanning();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function showCreateForm() {
		var id = getParameterByName('ValdistriktId');

		var selector = "#bemanningvaldistrikt-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "BemanningValdistriktForm",
				idString: "",
				parentIdString: id
			},
			selector: selector,
			openFunction: function () {
				$(selector + " .chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateBemanningValdistrikt";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getBemanning();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function toggleTackatJa(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/ToggleBemanningValdistriktTackatJa",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.ToggleBemanningValdistriktTackatJaResult.HasError) {
						getBemanning();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getSelectedValue(dataTag) {
		var val = [];
		$("#bemanning-results tbody tr").each(function (i) {
			val[i] = $(this).attr("data-" + dataTag);
		});

		return val;
	}

	function showEmail() {
		var val = getSelectedValue("email");

		var selector = "#epost-form";
		kaskelot.sp.ui.formutils.getAndShowPostForm({
			params: {
				formName: "EpostForm",
				idList: val,
			},
			selector: selector,
			openFunction: function () {
				$(".ui-dialog-buttonset .ui-button + .ui-button").hide();

				var doc = document;
				var text = doc.getElementById("Text");

				// Sätt text som markerad
				if (doc.body.createTextRange) { // ms
					var range = doc.body.createTextRange();
					range.moveToElementText(text);
					range.select();
				} else if (window.getSelection) { // moz, opera, webkit
					var selection = window.getSelection();
					var range = doc.createRange();
					range.selectNodeContents(text);
					selection.removeAllRanges();
					selection.addRange(range);
				}
			},
			okFunction: function () {
				$(selector).dialog("close");
			}
		});
	}

	function milChanged(id) {
		var mil = $("#mil-" + id).val();

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/UpdateErsattning",
			type: "GET",
			data: { id: id, mil: mil, taxi: null },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.UpdateErsattningResult.HasError) {
						getBemanning();
					}
					else {
						$("#mil-" + id).val("");
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function taxiChanged(id) {
		var taxi = $("#taxi-" + id).val();

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/UpdateErsattning",
			type: "GET",
			data: { id: id, mil: null, taxi: taxi },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.UpdateErsattningResult.HasError) {
						getBemanning();
					}
					else {
						$("#taxi-" + id).val("");
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	return {
		getValdistrikt: getValdistrikt,
		getBemanning: getBemanning,
		removePerson: removePerson,
		removeBemanning: removeBemanning,
		showCreateForm: showCreateForm,
		toggleTackatJa: toggleTackatJa,
		showEmail: showEmail,
		init: init,
		taxiChanged: taxiChanged,
		milChanged: milChanged
	}
}();